package com.slg.android.manyanime;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnChildClick;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.slg.android.manyanime.adapter.ChannelInfoAdapter;
import com.slg.android.manyanime.bean.ChannelInfo;
import com.slg.android.manyanime.callBackInterface.OnClickCallBack;
import com.slg.android.manyanime.utils.ApiUtils;
import com.slg.android.manyanime.utils.NetUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;


public class ChannelActivity extends FragmentActivity implements ViewTreeObserver.OnPreDrawListener, OnClickCallBack {
    @ViewInject(R.id.common_title_title_textView)
    private TextView title;
    @ViewInject(R.id.channel_animation_info)
    private TextView info;
    @ViewInject(R.id.channel_info_switch)
    private ImageView infoSwitch;
    @ViewInject(R.id.channel_animation_list)
    private RecyclerView recyclerView;
    private String id;
    private HttpUtils httpUtils;
    private List<ChannelInfo> data;
    private int lineCount;
    private boolean spread = false;//信息是否展开

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_channel);
        ViewUtils.inject(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        if (!TextUtils.isEmpty(id)) {
            ViewTreeObserver observer = info.getViewTreeObserver();
            observer.addOnPreDrawListener(this);
            if (NetUtil.isConnectInternet(this)) {
                httpUtils = new HttpUtils();
                httpUtils.send(HttpRequest.HttpMethod.GET
                        , ApiUtils.CHANNEL + id, new CallBack());
            }
        } else {
            title.setText("专题");
            infoSwitch.setVisibility(View.GONE);
        }


    }

    @OnClick({R.id.common_title_back_imageView, R.id.channel_info_switch})
    public void onClick(View view) {
        int viewId = view.getId();
        //todo 添加箭头的翻转动画
        switch (viewId) {
            case R.id.common_title_back_imageView:
                finish();
                break;
            case R.id.channel_info_switch:
                if (spread) {
                    info.setMaxLines(1);
                    spread = false;
                } else {
                    info.setMaxLines(lineCount);
                    spread = true;
                }
                break;
        }
    }

    @Override
    public boolean onPreDraw() {
        lineCount = info.getLineCount();
        return true;
    }

    @Override
    public void onItemClick(View v, int position) {
        ChannelInfo channelInfo = data.get(position);
        Intent intent = new Intent(this, AnimationDetailActivity.class);
        intent.putExtra("id",channelInfo.getVideoId());
        startActivity(intent);
    }

    private class CallBack extends RequestCallBack<String>  {

        @Override
        public void onSuccess(ResponseInfo<java.lang.String> stringResponseInfo) {
            String json = stringResponseInfo.result;
            data = ChannelInfo.parseData(json);
            try {
                JSONObject jsonObject = new JSONObject(json);
                JSONObject object = jsonObject.getJSONObject("info");
                String name = object.getString("name");
                String intro = object.getString("intro");
                info.setMaxLines(1);
                info.setText(intro);
                title.setText(name);
                if (data != null) {
                    ChannelInfoAdapter adapter = new ChannelInfoAdapter(ChannelActivity.this
                            , data);
                    adapter.setOnClickListener(ChannelActivity.this);
                    recyclerView.setAdapter(adapter);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onFailure(HttpException e, java.lang.String s) {

        }

    }
}
